package com.hualan.service.a;

import com.alibaba.fastjson.JSON;
import com.hualan.entity.a.MenDian;
import com.hualan.entity.a.XiaoQu;
import com.hualan.utils.EntityFactory;
import com.hualan.utils.ExecUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

@WebServlet(urlPatterns = {"/query","/queryByName","/queryByTime","/queryMenDian","/add","/update","/delete"})
public class XiaoquService extends HttpServlet {
    private List<XiaoQu> xiaoQuList;
    private List<MenDian> menDianList;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询
        if(req.getServletPath().equals("/query")){
            this.xiaoQuList=new ArrayList<>();
            String sql="select * from xiaoqu";
            Connection conn=(Connection) req.getAttribute("connection");
            try(Statement exec=conn.createStatement();
                ResultSet datas=exec.executeQuery(sql);) {
                while (datas.next()) {
                    EntityFactory entityFactory = new EntityFactory(XiaoQu.class);
                    XiaoQu xiaoQu = (XiaoQu) entityFactory.startCombine(datas);
                    this.xiaoQuList.add(xiaoQu);
                }
                resp.getWriter().write(JSON.toJSONString(this.xiaoQuList));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }else if(req.getServletPath().equals("/queryByName")){
            this.xiaoQuList=new ArrayList<>();
            String sql="select * from xiaoqu where name=?";
            List<Object> params = new ArrayList<>();
            params.add(req.getParameter("name"));
            try{
                ResultSet datas = (ResultSet) ExecUtil.execute(sql,params,req,false);
                while (datas.next()) {
                    EntityFactory entityFactory = new EntityFactory(XiaoQu.class);
                    XiaoQu xiaoQu = (XiaoQu) entityFactory.startCombine(datas);
                    this.xiaoQuList.add(xiaoQu);
                }
                resp.getWriter().write(JSON.toJSONString(this.xiaoQuList));
            }catch (Exception e){
                throw new RuntimeException(e);
            }finally {
                try {
                    ExecUtil.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }else if (req.getServletPath().equals("/queryByTime")){
            //a
            this.xiaoQuList=new ArrayList<>();
            String sql="select * from xiaoqu where time between ? and ?";
            List<Object> params=new ArrayList<>();
            params.add(req.getParameter("beginTime"));
            params.add(req.getParameter("endTime"));
            try{
                ResultSet datas = (ResultSet) ExecUtil.execute(sql,params,req,false);
                while (datas.next()) {
                    EntityFactory entityFactory = new EntityFactory(XiaoQu.class);
                    XiaoQu xiaoQu = (XiaoQu) entityFactory.startCombine(datas);
                    this.xiaoQuList.add(xiaoQu);
                }
                resp.getWriter().write(JSON.toJSONString(this.xiaoQuList));
            }catch (Exception e){
                throw new RuntimeException(e);
            }finally {
                try {
                    ExecUtil.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }else if(req.getServletPath().equals("/queryMenDian")){
            this.menDianList=new ArrayList<>();
            String sql="select m.* from xiaoqu x inner join mendian m on x.id=m.x_id where x.name=?";
            List<Object> params=new ArrayList<>();
            params.add(req.getParameter("name"));
            try{
                ResultSet datas=(ResultSet) ExecUtil.execute(sql,params,req,false);
                while (datas.next()){
                    EntityFactory entityFactory=new EntityFactory<>(MenDian.class);
                    MenDian menDian=(MenDian) entityFactory.startCombine(datas);
                    this.menDianList.add(menDian);
                }
                resp.getWriter().write(JSON.toJSONString(this.menDianList));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }finally {
                try {
                    ExecUtil.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //添加
        if(req.getServletPath().equals("/add")){
            req.setCharacterEncoding("utf-8");
            String name=req.getParameter("name");
            String location=req.getParameter("location");
            String time=req.getParameter("time");
            if(name!=null&&!name.trim().isEmpty()&&location!=null&&!location.trim().isEmpty()&&time!=null&&!time.trim().isEmpty()){
                String sql="insert into xiaoqu (name,location,time) values (?,?,?)";
                List<Object> params = new ArrayList<>();
                req.setCharacterEncoding("utf-8");
                params.add(name);
                params.add(location);
                params.add(time);
                Boolean result = (Boolean) ExecUtil.execute(sql,params,req,true);
                if(result){
                    resp.getWriter().write("添加成功");
                }
                try {
                    ExecUtil.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }else {
                resp.getWriter().write("添加失败,有字段为空");
            }
        }else if (req.getServletPath().equals("/update")){
            //更新
            req.setCharacterEncoding("utf-8");
            this.xiaoQuList=new ArrayList<>();
            String sqlA="select * from xiaoqu";
            Connection conn=(Connection) req.getAttribute("connection");
            try(Statement exec=conn.createStatement();
                ResultSet datas=exec.executeQuery(sqlA);) {
                Boolean bool=false;
                String id=req.getParameter("id");
                while (datas.next()) {
                    if(datas.getString("id").equals(id)){
                        bool=true;
                        break;
                    }
                }
                if(bool){
                    //开始更新
                    String name=req.getParameter("name");
                    String location=req.getParameter("location");
                    if(name!=null&&!name.trim().isEmpty()&&location!=null&&!location.trim().isEmpty()){
                        String sql="update xiaoqu set name=?,location=? where id=?";
                        List<Object> params=new ArrayList<>();
                        params.add(name);
                        params.add(location);
                        params.add(id);
                        Boolean result =(Boolean) ExecUtil.execute(sql,params,req,true);
                        if(result){
                            resp.getWriter().write("更新成功");
                        }
                        try {
                            ExecUtil.close();
                        } catch (SQLException e) {
                            throw new RuntimeException(e);
                        }
                    }else {
                        resp.getWriter().write("更新失败,有字段为空");
                    }
                }else {
                    resp.getWriter().write("更新失败,id不存在");
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        }else if(req.getServletPath().equals("/delete")){
            req.setCharacterEncoding("utf-8");
            this.xiaoQuList=new ArrayList<>();
            String sqlA="select * from xiaoqu";
            Connection conn=(Connection) req.getAttribute("connection");
            try (Statement exec = conn.createStatement();
                ResultSet datas=exec.executeQuery(sqlA)) {
                Boolean bool=false;
                String id=req.getParameter("id");
                while (datas.next()){
                    if(datas.getString("id").equals(id)){
                        bool=true;
                        break;
                    }
                }
                if(bool){
                    //开始删除
                    String sql="delete from xiaoqu where id=?";
                    List<Object> params=new ArrayList<>();
                    params.add(id);
                    Boolean result =(Boolean) ExecUtil.execute(sql,params,req,true);
                    if(result){
                        resp.getWriter().write("删除成功");
                    }
                    try {
                        ExecUtil.close();
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                }else {
                    resp.getWriter().write("删除失败,id不存在");
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //更新
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //删除
    }
}
